package com.bw.launcher

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.blankj.utilcode.util.BarUtils
import com.bw.launcher.navgraph.NavigationBuilder
import com.google.android.material.bottomnavigation.BottomNavigationView

class FrameActivity : AppCompatActivity(),BottomNavigationView.OnNavigationItemSelectedListener{

    private lateinit var frame_nav_bottom: BottomNavigationView
    private lateinit var frame_nav_group: NavHostFragment
    private lateinit var controller: NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view = LayoutInflater.from(this).inflate(R.layout.activity_frame,null,false)
        setContentView(view)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = (
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
        }
        BarUtils.addMarginTopEqualStatusBarHeight(view)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        initView()
    }

    private fun initView(){
        frame_nav_bottom = findViewById(R.id.frame_nav_bottom)
        frame_nav_group =
            (supportFragmentManager.findFragmentById(R.id.frame_nav_group) as NavHostFragment?)!!
        controller = frame_nav_group!!.navController
        NavigationBuilder.builder(this,supportFragmentManager,controller!!,
            frame_nav_group!!.id)
        frame_nav_bottom!!.setOnNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        controller!!.navigate(item.itemId)
        return true
    }

}